

import requests
from loguru import logger

COOKIE = "reusername=13544276652; repassword=0oOo%29_%28_yxsrC2sbGztLe3trM%3D; jys2frontsid=959b06fd-24ea-4843-862f-d9b81e08a13b"

QUERY_JSON_ALL = {
    "pageCurrent": 1,
    "pageSize": 5,
    "yxmc": "",
    "yxsd": "",
    "rmcs": "",
    "bxlb": "",
    "xxlb": "",
    "lqpc": "1",
    "sf985": 0,
    "sf211": 0,
    "sfsyl": 0,
    "sfqt": 0,
    "xzkm": "3,4",

    "minzdtwf": "300",
    "maxzdtwf": "750",

    "xzyx": "0",
    # 是否中外合作
    "sfbhzwhz": "0",
    # 是否国家专项计划
    "sfbhgjzx": "1",
    # 是否地方专项计划
    "sfbhdfzx": "0",
    "sfbhssykb": "1",
    "sfbhmzb": "1",
    "sfbhcygj": "1"
}

def get_single_page_school_detail(query_json):
    r = requests.post(
        "https://www.dxlql.com/schoolMajor/selectSchoolMajorPage",
        headers={
            "Content-Type": "application/json;charset=UTF-8",
            "Cookie": COOKIE,
        },
        json=query_json
    )
    return r

def get_all_school_detail():
    result = []
    cur_query_json = QUERY_JSON_ALL.copy()

    total_page = float("inf")
    cur_page = 2
    
    while cur_page <= total_page + 2:
        cur_query_json["pageCurrent"] = cur_page
        r = get_single_page_school_detail(cur_query_json)
        _content = r.json()

        
        _content["page_idx"] = cur_page
        result.append(_content)
        
        total_page = r.json()["data"]["totalPage"]
        logger.info(f"req page: [{cur_page} / {total_page}]")
        cur_page += 1
    return result


def get_zyz_detail(zyz_id):
    r = requests.get(
        f"https://www.dxlql.com/schoolMajor/searchZyzMajorList//{zyz_id}",
        headers={
            "Content-Type": "application/json;charset=UTF-8",
            "Cookie": COOKIE,
        },
    )
    return r.json()